package com.ideabove.adapter;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;

import com.ideabove.network.NetWorkManager;
import com.ideabove.radio.MainActivity;
import com.ideabove.radio.R;
import com.ideabove.radio.module.FacebookMd;
import com.ideabove.radio.module.FolderEditMd;
import com.ideabove.util.ConnectThread;
import com.ideabove.util.img.ImageLoader;
import com.ideabove.custom.MyCropImageView;
import com.ideabove.data.Channel;
import com.ideabove.data.Folder;
import com.ideabove.data.PlayItem;
import com.ideabove.data.Song;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ScaleXSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;
import com.ideabove.util.img.BitmapDownloaderTask;

public class SongAdapter extends BaseAdapter
{

	private List<PlayItem> mList;
	private LayoutInflater mInflater;  
	private Context context;
	private MainActivity activity;
	private boolean isEditable = false; // 1: edit mode
	private int index = 0;

	public SongAdapter(Context c , ArrayList<PlayItem> arrays, MainActivity activty){
		this.mInflater = LayoutInflater.from(c);
		this.mList = arrays;
		this.context = c;
		this.activity = activty;
	}

	public void setEditMode(boolean isEditable){
		this.isEditable = isEditable;
	}
	
	public boolean isEditMode(){
		return isEditable; 
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		if(convertView == null)
		{
			convertView = mInflater.inflate(R.layout.row_folder_detail, null);			
		}

		final PlayItem song = mList.get(position);
		final View rowView = convertView;
		final int rowPos = position;
		if (song != null) {
			// playitem을 넣어줌
			convertView.setTag(song);
			
			MyCropImageView songImg = (MyCropImageView) convertView.findViewById(R.id.song_img);
			songImg.setImageResource(R.drawable.black);
			songImg.setScaleType(ScaleType.FIT_XY);
			songImg.setImageUrl(song.getThumbUrl());			
			TextView songTitle = (TextView) convertView.findViewById(R.id.song_title);
			songTitle.setText(song.getTitle());
			
			
			if(isEditable){
				FrameLayout song_remove_btn_wrap = (FrameLayout) convertView.findViewById(R.id.song_remove_btn_wrap);
				song_remove_btn_wrap.setVisibility(View.VISIBLE);
				
				ImageView song_remove_btn = (ImageView) convertView.findViewById(R.id.song_remove_btn);
				song_remove_btn.setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						ImageView view = (ImageView)v;
						
						
						if(event.getAction() == MotionEvent.ACTION_DOWN){
							view.setPadding(2, 2, 2, 2);
							view.setColorFilter(0xaa111111,Mode.SRC_OVER);				
						}else if(event.getAction() == MotionEvent.ACTION_UP){
																				
							// code							
							activity.folderDetailMd.requestRemoveSong(rowView ,rowPos, song);
							view.setPadding(0, 0, 0, 0);
							view.setColorFilter(0x00000000, Mode.SRC_OVER);
						}else{
							view.setPadding(0, 0, 0, 0);
							view.setColorFilter(0x00000000, Mode.SRC_OVER);
						}
						return true;
					}
				});
			}
			
		}
		
		Animation animation = AnimationUtils.loadAnimation(activity, android.R.anim.slide_in_left);
		convertView.startAnimation(animation);

		return convertView;
	}

	@Override
	public int getCount() {
		return mList.size();
	}
	@Override
	public PlayItem getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
